package com.zhuoyue.biz.core.command.cmd.common.fe6;


import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.command.cmd.common.fe6.base.BeiJingYinYueStateType;
import com.zhuoyue.biz.core.command.constant.BeiJingYinYueFhwise0Fe60203CmdDeal;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.utils.CmdUtils;


import java.util.Map;

/**
 * 背景音乐器
 * @author Administrator
 */
public class BeiJingYinYueFhwise0Fe60203 {

    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {

        String cmd = "";
        switch (controlReq.getControlType()) {
            //获取背景音乐ID号
            case ControlType.GET_BACK_GROUD_ID_REQUEST:
                cmd = getIdCmd(deviceAndGatewayDO);
                break;
            //控制
            case ControlType.CONTROL_BACK_GROUD_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, controlReq);
                break;
            default:
                break;
        }
        return cmd;
    }

    /**
     * 获取设备控制数据帧
     *
     * @param deviceAndGatewayDO
     * @param controlReq
     * @return
     */
    private static String getControlCmd(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {

        String gatewayMac = deviceAndGatewayDO.getGatewayMac();
        String deviceMac = deviceAndGatewayDO.getZigbeeMac();


        Map<String, Object> actionsMap = controlReq.getActions();
        String fhwiseId = actionsMap.get("fhwiseId").toString();

        //state：  开机：OPEN  关机：CLOSE  开始播放：START  暂停：STOP  上一首：LAST  下一首：NEXT  音量-：VOLUME_DEL  音量+：VOLUME_ADD
        String state = actionsMap.get("state").toString();

        String controlData = "";
        switch (state) {
            case BeiJingYinYueStateType.OPEN:
                controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_OPEN;
                break;
            case BeiJingYinYueStateType.CLOSE:
                controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_CLOSE;
                break;
            case BeiJingYinYueStateType.START:
                controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_START;
                break;
            case BeiJingYinYueStateType.STOP:
                controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_STOP;
                break;
            case BeiJingYinYueStateType.LAST:
                controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_LAST;
                break;
            case BeiJingYinYueStateType.NEXT:
                controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_NEXT;
                break;
            case BeiJingYinYueStateType.VOLUME_ADD:
                controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_VOLUME_ADD;
                break;
            case BeiJingYinYueStateType.VOLUME_DEL:
                controlData = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_4_VOLUME_DEL;
            default:
                break;
        }


        String data = fhwiseId + BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_3 + controlData;

        String sum = CmdUtils.getCmdBodySum(data);

        //获取背景音乐协议的完成控制指令
        String controlCmd = BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_1 +
                data +
                sum +
                BeiJingYinYueFhwise0Fe60203CmdDeal.ControlCmd.CMD_6;


        String cmd3To8 = BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_3 +
                BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_4 +
                gatewayMac +
                deviceMac +
                CmdUtils.getDateLength(controlCmd) +
                controlCmd;

        String cmd2 = CmdUtils.getDateLength(cmd3To8);

        String cmd9 = CmdUtils.getCmdBodySum(cmd3To8);

        String fullCmd = BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_1 +
                cmd2 +
                cmd3To8 +
                cmd9 +
                BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_10;

        return fullCmd;

    }

    /**
     * 获取背景音乐ID号
     *
     * @param deviceAndGatewayDO
     * @return
     */
    private static String getIdCmd(DeviceAndGatewayDO deviceAndGatewayDO) {

        String gatewayMac = deviceAndGatewayDO.getGatewayMac();
        String deviceMac = deviceAndGatewayDO.getZigbeeMac();

        String cmd3To8 = BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_3 +
                BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_4 +
                gatewayMac +
                deviceMac +
                CmdUtils.getDateLength(BeiJingYinYueFhwise0Fe60203CmdDeal.GetIdCmd.CMD_1) +
                BeiJingYinYueFhwise0Fe60203CmdDeal.GetIdCmd.CMD_1;

        String cmd2 = CmdUtils.getDateLength(cmd3To8);

        String cmd9 = CmdUtils.getCmdBodySum(cmd3To8);

        String fullCmd = BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_1 +
                cmd2 +
                cmd3To8 +
                cmd9 +
                BeiJingYinYueFhwise0Fe60203CmdDeal.CommonCmd.CMD_10;

        return fullCmd;
    }

}
